var app = angular.module("app",[
    "ui.bootstrap",
    "ui.bootstrap.tpls"
]);

// 请求的根路径，你可以更改此路径以修改测试的api环境
var requestRootUrl = "/";

/**
 * 定义数据的服务。这个服务实现全部请求的发送。
 */
app.service("manageTeacherDataService",[
    "$http",
    function ($http) {
        return {
            // 查询所有的导师
            getAllTeacher:function () {
                return $http
                    .get(requestRootUrl + "api/manage-teacher/all-teacher");
            },
            // 添加一个导师
            addTeacher:function (teacher) {
                return $http
                    .post(requestRootUrl + "api/manage-teacher/add-teacher",teacher);
            },
            // 修改一个导师
            updateTeacher:function (teacher) {
                return $http
                    .post(requestRootUrl + "api/manage-teacher/update-teacher",teacher);
            },
            // 删除一个导师
            deleteTeacher:function (teacher) {
                return $http
                    .post(requestRootUrl + "api/manage-teacher/delete-teacher", teacher);
            }
        }
    }
]);

/**
 * 这个demo使用了angular ui bootstrap这个ui库。
 * 你可以看到有$uibModal.open这样的用法
 */
app.controller("manageTeacherController", [
    "$scope",
    "manageTeacherDataService",
    "$uibModal",
    function ($scope, manageTeacherDataService, $uibModal) {

        // 这个属性用于显示数据的加载状态
        // 绑定到界面上，以控制div的显示
        // 绑定位置类似 ng-if="state =='loading'"
        $scope.state = "loading";

        manageTeacherDataService
            .getAllTeacher()
            // 请求是异步的，调用then设置请求成功的事件
            .then(
                function success(resp) {
                    $scope.state = "finished";

                    // 赋值所有的导师
                    // angular会根据列表进行渲染
                    $scope.allTeachers = resp.data;

                    // 打开添加导师的对话框
                    $scope.addTeacher = function () {
                        $uibModal.open({
                            backdrop: "static",
                            templateUrl: "add-teacher-dialog.html",
                            size: "sm",
                            controller: "addTeacherDialogController",
                            resolve: {
                                allTeachers: function () {
                                    return $scope.allTeachers;
                                }
                            }
                        })
                    };

                    // 打开修改导师的对话框
                    $scope.updateTeacher = function (teacher) {
                        $uibModal.open({
                            backdrop: "static",
                            templateUrl: "update-teacher-dialog.html",
                            size: "sm",
                            controller: "updateTeacherDialogController",
                            resolve: {
                                thisTeacher: function () {
                                    return teacher;
                                }
                            }
                        });
                    };

                    // 打开删除导师的对话框
                    $scope.deleteTeacher = function (teacher) {
                        $uibModal.open({
                            backdrop: "static",
                            templateUrl: "delete-teacher-dialog.html",
                            size: "sm",
                            controller: "deleteTeacherDialogController",
                            resolve: {
                                allTeachers: function () {
                                    return $scope.allTeachers;

                                },
                                thisTeacher: function () {
                                    return teacher;
                                }
                            }
                        });
                    };

                }, function error(resp) {
                    $scope.state = "error";
                });
    }]);

// 添加导师对话框
app.controller("addTeacherDialogController", [
    "$scope",
    "manageTeacherDataService",
    "allTeachers",
    "$uibModalInstance",
    function ($scope, manageTeacherDataService, allTeachers, $uibModalInstance) {

        $scope.tutorTitles = [
            "教授",
            "副教授",
            "讲师",
            "助理教授"
        ];

        $scope.newTeacher = {
            tutorName: "",
            tutorTitle: "",
            phoneNumber: "",
            email: ""
        };


        $scope.dismiss = function () {
            $uibModalInstance.dismiss();
        };

        // 标示请求是否结束
        $scope.isDone = true;
        $scope.submit = function () {
            $scope.isDone = false;
            manageTeacherDataService
                .addTeacher($scope.newTeacher)
                .then(
                    function success(resp) {
                        allTeachers.push($scope.newTeacher);

                        $uibModalInstance.dismiss();

                        $scope.isDone = true;
                    },
                    function error(resp) {
                        alert("好像出了点问题");

                        $scope.isDone = true;
                    }
                )
        };
    }
]);

// 修改导师对话框
app.controller("updateTeacherDialogController", [
    "$scope",
    "manageTeacherDataService",
    "$uibModalInstance",
    "thisTeacher",
    function ($scope, manageTeacherDataService, $uibModalInstance, thisTeacher) {

        $scope.tutorTitles = [
            "教授",
            "副教授",
            "讲师",
            "助理教授"
        ];

        $scope.tempTeacher = {};
        // 深拷贝这个对象进行暂存
        angular.copy(thisTeacher, $scope.tempTeacher);

        $scope.dismiss = function () {
            $uibModalInstance.dismiss();
        };

        // 请求是否结束
        $scope.isDone = true;
        $scope.submit = function () {

            $scope.isDone = false;
            manageTeacherDataService
                .updateTeacher($scope.tempTeacher)
                .then(
                    function success(resp) {
                        angular.copy($scope.tempTeacher, thisTeacher);

                        $uibModalInstance.dismiss();

                        $scope.isDone = true;
                    },
                    function error(resp) {
                        alert("好像出了点问题");

                        $scope.isDone = true;
                    }
                )
        };


    }
]);

app.controller("deleteTeacherDialogController", [
    "$scope",
    "manageTeacherDataService",
    "thisTeacher",
    "allTeachers",
    "$uibModalInstance",
    "$http",
    function ($scope, manageTeacherDataService, thisTeacher, allTeachers, $uibModalInstance,$http) {
        $scope.dismiss = function () {
            $uibModalInstance.dismiss();
        };

        // 请求是否结束
        $scope.isDone = true;
        $scope.submit = function () {
            $scope.isDone = false;
            manageTeacherDataService
                .deleteTeacher(thisTeacher)
                .then(
                    function success(resp) {
                        allTeachers.splice(allTeachers.lastIndexOf(thisTeacher), 1);

                        $uibModalInstance.dismiss();

                        $scope.isDone = true;
                    },
                    function error(resp) {
                        alert("好像出了点问题");

                        $scope.isDone = true;
                    }
                );
        }
    }
]);